import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

/// 获取原生存的用户数据 工具类
class KeyValueNativeUtil {
  static const _methodChannelName = 'flutter.keqiang.com/kv';

  static const _platformMethod = MethodChannel(_methodChannelName);

  /// 获取bool类型
  static Future<bool> getBool({required String key, bool? defValue}) async {
    try {
      return await _platformMethod
          .invokeMethod('getBool', {'key': key, 'defaultValue': defValue});
    } on PlatformException catch (e) {
      debugPrint(e.message);
      return defValue ?? false;
    }
  }

  /// 获取String类型
  static Future<String?> getString(
      {required String key, String? defValue}) async {
    try {
      return await _platformMethod
          .invokeMethod('getString', {'key': key, 'defaultValue': defValue});
    } on PlatformException catch (e) {
      debugPrint(e.message);
      return defValue;
    }
  }

  /// 获取Int类型
  static Future<int> getInt({required String key, int? defValue}) async {
    try {
      return await _platformMethod
          .invokeMethod('getInt', {'key': key, 'defaultValue': defValue});
    } on PlatformException catch (e) {
      debugPrint(e.message);
      return defValue ?? 0;
    }
  }
}
